﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Triggers;
using BussinesLayer.Services;

namespace Mvc4.Others
{
    public class Watchdog : IJob
    {
        private UserServices us = new UserServices();

        public Watchdog() { }

        public void Execute(IJobExecutionContext context)
        {
            us.Watchdog();
            Console.WriteLine("Watchdog exectued at " + DateTime.Now.ToString());
        }
    }

    public static class WatchdogScheduler
    {
        public static void StartWatchdogScheduler()
        {
            ISchedulerFactory schedFact = new StdSchedulerFactory();
            IScheduler sched = schedFact.GetScheduler();
            sched.Start();
            
            JobDetailImpl jobDetail = new JobDetailImpl("Watchdog", null, typeof(Watchdog));
            SimpleTriggerImpl trigger = new SimpleTriggerImpl("WatchdogTrigger", null, DateTime.UtcNow, null, SimpleTriggerImpl.RepeatIndefinitely, TimeSpan.FromMinutes(5));

            sched.ScheduleJob(jobDetail, trigger);
        }
    }
}